package com.cems.service.impl;

import com.cems.mapper.EntrustMapper;
import com.cems.pojo.SysEntrust;
import com.cems.service.EntrustService;
import com.cems.util.RedisUtil;
import com.cems.util.SelectRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EntrustServiceImpl
 * @Author 陈新予(blank)
 * @Date 2021/5/20
 * @Version 1.0
 */
@Service
public class EntrustServiceImpl implements EntrustService {


    @Override
    public void delLeisureEntrustById2(Integer id) {
        entrustMapper.delLeisureEntrustById2(id);
        redisUtil.delete("cems_Entrust");
    }

    //判断属性是否重复
    @Override
    public boolean judgeTypeRep(String entrustType) {
        System.out.println("entrustTypeentrustType+" + entrustType);
//        redisUtil.delete("cems_AllEtrustType");
        List<SysEntrust> allEtrustType = entrustMapper.getAllEtrustType();
        LinkedList<String> list = new LinkedList<>();
        for (SysEntrust type : allEtrustType) {
            System.err.println(type.getEntType().equals(entrustType));
            list.addLast(type.getEntType());
        }
        if (list.contains(entrustType)) {
            System.err.println(entrustType + "有重复的");
            return false;
        } else {
//            entrustMapper.addEntrustType(entrustType);
            System.err.println(entrustType + "无重复");
            return true;
        }
    }

    @Override
    public List<SysEntrust> getThreeEntrusts() {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_ThreeEntrusts", entrustMapper, "getThreeEntrusts");
            ArrayList<SysEntrust> entrusts = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysEntrust)
                    entrusts.add((SysEntrust) object);
            }
            return entrusts;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return entrustMapper.getThreeEntrusts();
        }
    }

    @Transactional
    @Override
    public void addEntrustType(String entrustType) {
        redisUtil.delete("cems_AllEtrustType");
        List<SysEntrust> allEtrustType = entrustMapper.getAllEtrustType();
        System.err.println(allEtrustType);
        System.out.println(entrustType);
        LinkedList<String> list = new LinkedList<>();
        for (SysEntrust type : allEtrustType) {
            System.err.println(type.getEntType().equals(entrustType));
            list.addLast(type.getEntType());
        }
        if (list.contains(entrustType)) {
            System.out.println(entrustType + "插入失败");
        } else {
            entrustMapper.addEntrustType(entrustType);
        }
    }


    @Autowired
    RedisUtil redisUtil;
    @Autowired
    EntrustMapper entrustMapper;


    @Override
    public List<SysEntrust> getEntrusts() {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_Entrust", entrustMapper, "getEntrusts");
            ArrayList<SysEntrust> entrusts = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysEntrust)
                    entrusts.add((SysEntrust) object);
            }
            return entrusts;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return entrustMapper.getEntrusts();
        }

    }

    @Override
    public List<SysEntrust> getLeisureEntrusts() {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_LeisureEntrust", entrustMapper, "getLeisureEntrusts");
            ArrayList<SysEntrust> entrusts = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof SysEntrust)
                    entrusts.add((SysEntrust) object);
            }
            return entrusts;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return entrustMapper.getLeisureEntrusts();
        }
    }

    @Override
    public void GetEntrust(int id, int userId) {
        entrustMapper.GetEntrust(id, userId);
        redisUtil.delete("cems_LeisureEntrust");
        redisUtil.delete("cems_Entrust");
    }

    @Override
    public void userRevEntrustStatus(int id, String status) {
        entrustMapper.userRevEntrustStatus(id, status);
        redisUtil.delete("cems_LeisureEntrust");
        redisUtil.delete("cems_Entrust");
    }

    @Override
    public List<SysEntrust> getMyEntIng(int id) {
        return entrustMapper.getMyEntIng(id);
    }

    @Override
    public List<SysEntrust> getMyEntEnd(int id) {
        return entrustMapper.getMyEntEd(id);
    }

    @Override
    public List<String> getEntrustTypes() {
        try {
            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_LeisureEntrustTypes", entrustMapper, "getEntrustTypes");
            ArrayList<String> entrustTypes = new ArrayList<>();
            for (Object object : objects) {
                if (object instanceof String)
                    entrustTypes.add((String) object);
            }
            return entrustTypes;
        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
            e.printStackTrace();
            return entrustMapper.getEntrustTypes();
        }
    }

    @Override
    public int insertEntrust(Map<String, Object> map) {
        return entrustMapper.insertEntrust(map);
    }

    @Override
    public int getEntrustTypeByType(String entType) {
        return entrustMapper.getEntrustTypeByType(entType);
    }

    @Override
    public List<SysEntrust> getLeisureEntrustsById(int entConsignor) {
//        try {
//            List<Object> objects = SelectRedis.selectRedis(redisUtil, "cems_LeisureEntrustsById", entrustMapper, "getLeisureEntrustsById");
//            ArrayList<SysEntrust> entrusts = new ArrayList<>();
//            for (Object object : objects) {
//                if (object instanceof SysEntrust)
//                    entrusts.add((SysEntrust) object);
//            }
//            return entrusts;
//        } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
//            e.printStackTrace();
//            return entrustMapper.getLeisureEntrustsById(entConsignor);
//        }
        return entrustMapper.getLeisureEntrustsById(entConsignor);
    }

    //删除
    @Override
    public int delLeisureEntrustById(int id) {
       return entrustMapper.delLeisureEntrustById(id);
    }

    //修改
    @Override
    public int revLeisureEntrustById(Map<String, Object> map) {
        return entrustMapper.revLeisureEntrustById(map);
    }

    //根据委托id查询
    @Override
    public SysEntrust getLeisureEntrustsByEntId(int id) {
        return entrustMapper.getLeisureEntrustsByEntId(id);
    }

    @Override
    public List<SysEntrust> getLeisureEntrustsById2(int entConsignor) {
        return entrustMapper.getLeisureEntrustsById2(entConsignor);
    }

    @Override
    public List<SysEntrust> AlreadyLeisureEntrustsByEntId(int id) {
        return entrustMapper.AlreadyLeisureEntrustsByEntId(id);
    }

    @Override
    public int OkrevEntrustState(int id) {
        return entrustMapper.OkrevEntrustState(id);
    }

    @Override
    public int NorevEntrustState(int id) {
        return entrustMapper.NorevEntrustState(id);
    }

    @Override
    public List<String> getEntrustName() {
        return entrustMapper.getEntrustName();
    }


    // //委托类型中 存在真正委托的查询
    @Override
    public List<String> getExistEnt() {
        return entrustMapper.getExistEnt();
    }

    /**
     * 未完成的订单退款
     *
     * @param map
     * @return
     */
    @Override
    public int upQuitEtrustEntMoney(Map<String, Object> map) {
        redisUtil.delete("cems_AllUsers");
        redisUtil.delete("cems_AllUsers");
        redisUtil.delete("cems_UserMessages");
        return entrustMapper.upQuitEtrustEntMoney(map);

    }

    //委托类型中 存在真正委托的查询
    @Override
    public List<Integer> getExistEntNums() {
        return entrustMapper.getExistEntNums();
    }

    @Override
    public List<Integer> getEntPlanNums() {
        return entrustMapper.getEntPlanNums();
    }

    @Override
    public List<String> getEntPlan() {
        return entrustMapper.getEntPlan();
    }

    @Override
    public List<Integer> getTypeNums() {
        return entrustMapper.getTypeNums();
    }
}
